﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Kaixin.Components.Tycoon.DTO;

namespace Kaixin.Components.Tycoon.DAL
{
    class GoodsDAL: Persistence<Goods>
    {
        public override string XML_FILE_PATH
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory+"DATABASE_v0.2.XML";
            }
        }


        public override void InitItem(Goods goods)
        {
            XElement el = SearchItem(goods);
            if (el == null)
            {
                throw new Exception("未找到goods数据");
            }
            else
            {
                goods.LastPrice = long.Parse(el.Element("last").Value);
                goods.MinPrice = long.Parse(el.Element("min").Value);
                goods.MaxPrice = long.Parse(el.Element("max").Value);
            }
        }

        protected override XElement SearchItem(Goods goods)
        {
            var s = from el in _xd.Element("root").Elements("product") where el.Element("ID").Value == goods.ID select el;
            return s.FirstOrDefault();
        }

        public override void AddItem(Goods goods)
        {
            XElement price = new XElement("price", goods.CurrentPrice);
            price.SetAttributeValue("count", "1");//该价格出现过1次
            _xd.Element("root").Add(new XElement("product"
                           , new XElement("ID", goods.ID)
                           , new XElement("min", goods.MinPrice)
                           , new XElement("max", goods.MaxPrice)
                           , new XElement("name", goods.Name)
                           , new XElement("last", goods.LastPrice)
                           , new XElement("prices",price)
                           ));
        }

        public override void FixItem(Goods goods)
        {
            XElement el = SearchItem(goods);
            if (el == null)
            {
                AddItem(goods);
            }
            else
            {
                el.Element("min").Value = goods.MinPrice.ToString();
                el.Element("max").Value = goods.MaxPrice.ToString();
                el.Element("last").Value = goods.LastPrice.ToString();
                var priceEL = (from x in el.Element("prices").Elements("price") where x.Value == goods.CurrentPrice.ToString() select x).FirstOrDefault();
                if (priceEL == null)
                {
                    XElement price = new XElement("price", goods.CurrentPrice);
                    price.SetAttributeValue("count", "1");//该价格出现过1次
                    el.Element("prices").Add(price);
                }
                else
                {
                    int count = int.Parse(priceEL.Attribute("count").Value);
                    priceEL.SetAttributeValue("count", count+1);
                }
            }
        }

        public override void DeleteItem(Goods item)
        {
            throw new NotImplementedException();
        }

        public override List<Goods> GetAllItem()
        {
            List<Goods> list = new List<Goods>();
            foreach (var item in _xd.Element("root").Elements("product"))
            {
                list.Add(Exchange(item));
            }
            return list;
        }

        private Goods Exchange(XElement el)
        {
            Goods g = new Goods();
            g.ID = el.Element("ID").Value;
            g.MaxPrice =long.Parse(el.Element("max").Value);
            g.MinPrice = long.Parse(el.Element("min").Value);
            g.Name = el.Element("name").Value;
            g.LastPrice = long.Parse(el.Element("last").Value);
            return g;
        }

       
    }
}
